import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.*;

/**
 *
 * @author Dell
 */
public class ViewEntry extends javax.swing.JPanel {
	JPanel mainPanel;
    Connection con;
    private String titleValue = null;

    /** Creates new form DeleteEntry */
    public ViewEntry(JPanel panel) {
        initComponents();
        mainPanel = panel;
        
        try {
            con = DriverManager.getConnection("jdbc:derby://localhost:1527/user", "user", "user");
            System.out.println("DB connected from ViewEntry");
            
        } catch (SQLException ex) {
        	//Logger.getLogger(ConnectingDatabase.class.getName()).log(Level.SEVERE, null, ex);
        	System.out.println(ex.getMessage());
        }
        
        okButton.addActionListener(new PanelSwitchToEntryMenu());
      }

    @SuppressWarnings("unchecked")
    private void initComponents() {

    	viewTitleComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        title = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();
        venueLabel = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionTextArea = new javax.swing.JTextArea();

        setPreferredSize(new java.awt.Dimension(650, 650));

        viewTitleComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose Entry Title" }));
        viewTitleComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	viewTitleComboBoxActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("View Entry");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        title.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        title.setText("Title :");

        timeLabel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        venueLabel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
       
        descriptionTextArea.setEditable(false);
        descriptionTextArea.setEnabled(true);

        okButton.setText("OK");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Time :");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Venue :");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText(" Description :");

        descriptionTextArea.setColumns(20);
        descriptionTextArea.setRows(5);
        jScrollPane1.setViewportView(descriptionTextArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(87, 87, 87)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(title)
                            .addComponent(jLabel4))
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(venueLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                            .addComponent(timeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                            .addComponent(viewTitleComboBox, 0, 350, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE))))
                .addGap(107, 107, 107))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(64, 64, 64)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(viewTitleComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(title))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(timeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(venueLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(okButton)
                .addGap(83, 83, 83))
        );
    }
    
    private void viewTitleComboBoxActionPerformed(ActionEvent evt) {
    	String venueValue = null , descriptionValue = null, getEntryTime = null;
    	String hour = null, minute = null, amPM = null;
    	int checkHour = 0;
    	
    	titleValue = (String)viewTitleComboBox.getSelectedItem();
    	
    	try{
    		Statement stmt = con.createStatement();
    		ResultSet rset = stmt.executeQuery("SELECT * FROM ENTRY WHERE E_TITLE LIKE '" + titleValue + "'");
    		
    		
    		while(rset.next()) {
    			getEntryTime = rset.getString(4);
    			venueValue = rset.getString(5);
    			descriptionValue = rset.getString(6);
    		}  
    		
    		stmt.close();
    		
    		
    		//Change the hour from 24 hour system back to 12 hour system
    		checkHour = Integer.parseInt(getEntryTime) / 100;
			if(checkHour >= 12 && checkHour < 24) {
				if(checkHour > 12) {
					checkHour -= 12;
					if(checkHour < 10)
						hour = ("0" + Integer.toString(checkHour));
					else
						hour = Integer.toString(checkHour);
				}
				else
					hour = "12";
				amPM = "PM";
			}
			else {
				if(checkHour < 10)
					hour = ("0" + Integer.toString(checkHour));
				else if(checkHour == 24)
					hour = "00";
				else
					hour = Integer.toString(checkHour);
				amPM = "AM";
			}
			
			//Change the minutes from 24 hour system back to 12 hour system
			minute = Integer.toString(Integer.parseInt(getEntryTime) % 100);
    		if(Integer.parseInt(minute) < 10)
    			minute = "0" + minute;
    		else if(Integer.parseInt(minute) == 60)
    			minute = "00";
    		
    		//Concatenate the hour, minute and AM/PM together
    		getEntryTime = hour + ":" + minute + " " + amPM;
    		
    		timeLabel.setText(getEntryTime);
    		venueLabel.setText(venueValue);
    		descriptionTextArea.setText(descriptionValue);
    	}
    	catch(Exception e){}
    }

    
    class PanelSwitchToEntryMenu implements ActionListener {
		public void actionPerformed(ActionEvent ae) {
			viewTitleComboBox.setSelectedIndex(0);
			
			timeLabel.setText(null);
    		venueLabel.setText(null);
    		descriptionTextArea.setText(null);
    		
			CardLayout c = (CardLayout)(mainPanel.getLayout());
			c.show(mainPanel, "EntryMenu");
		}
	}
    
    
    private JTextArea descriptionTextArea;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JScrollPane jScrollPane1;
    private JButton okButton;
    static  JComboBox viewTitleComboBox;
    private JLabel title;
    private JLabel timeLabel;
    private JLabel venueLabel;
}
